
//
//  BFAudioPLayer.m
//  beifang
//
//  Created by Apple on 17/4/25.
//  Copyright © 2017年 郭凯. All rights reserved.
//

#import "BFAudioPLayer.h"
#import <AVFoundation/AVFoundation.h>

@interface BFAudioPLayer ()<AVAudioPlayerDelegate>

@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@property (nonatomic,strong) AVPlayer* avplayer;
@end

@implementation BFAudioPLayer
- (instancetype)init
{
    self = [super init];
    if (self) {
//        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    }
    return self;
}
/**播放音频文件*/
-(void)playAudio:(NSURL *)url{
    NSError *error;
    self.audioPlayer =[[ AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    NSLog(@"url=%@  error=%@",url,error);
    self.audioPlayer.delegate = self; 
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer play];
}
/**播放网络在线音乐*/
-(void)playOnlineAudio:(NSURL *)url{
    //方法一
//    NSData *mydata=[[NSData alloc]initWithContentsOfURL:url];
//    NSLog(@"%@",mydata);
//    self.audioPlayer=[[AVAudioPlayer alloc]initWithData:mydata error:nil];
//    [self.audioPlayer prepareToPlay];
//    [self.audioPlayer play];
    
    //方法二
    AVPlayer *player = [[AVPlayer alloc]initWithURL:url];
    self.avplayer = player;
    [player play];

}
/**暂停音频文件播放*/
-(void)pasue{
    [self.audioPlayer pause];
    [self.avplayer pause];
}
/**恢复播放*/
-(void)resume{
    [self.audioPlayer play];
}
/**结束*/
-(void)stop{
    [self.audioPlayer stop];
}
#pragma mark -AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"音频播放完毕");
}
@end
